package com.example.service;

import com.example.util.JacksonUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

@Slf4j
@Service
public class KafkaProducerService {

    public static final String TOPIC = "control";
    @Resource
    private KafkaTemplate<String, String> kafkaTemplate;

    public void sendMessage(MessageBody messageBody) {
        String message = JacksonUtil.encode(messageBody);
        kafkaTemplate.send(TOPIC, message);
//        log.info("Sent message: " + message);
    }

    /**
     * LED 是否开启
     * 风扇 PWM 占空比 0 ~ 10
     * 蜂鸣器 是否响声
     * 步进电机
     */
    @Data
    public static class MessageBody {
        private boolean greenLed = false;
        private boolean orangeLed = false;
        private int fans = 0;
        private int buzzer = 0;
        private SteppingMotor steppingMotor;

        public static MessageBody newInstance() {
            MessageBody messageBody = new MessageBody();
            messageBody.setGreenLed(GreenLedService.STATUS_GREEN_LED_LIST.get(0));
            messageBody.setOrangeLed(OrangeLedService.STATUS_ORANGE_LED_LIST.get(0));
            messageBody.setFans(FanService.STATUS_FANS_LIST.get(0));
            messageBody.setBuzzer(BuzzerService.STATUS_BUZZER_LIST.get(0));
            messageBody.setSteppingMotor(StepperMotorService.STATUS_STEPPER_MOTOR_LIST.get(0));
            return messageBody;
        }

        public static boolean same(MessageBody messageBody1, MessageBody messageBody2) {
            if (Objects.isNull(messageBody1) && Objects.isNull(messageBody2)) {
                return true;
            } else if (Objects.isNull(messageBody1) || Objects.isNull(messageBody2)) {
                return false;
            } else {
                return Objects.equals(messageBody1.isGreenLed(), messageBody2.isGreenLed())
                        && Objects.equals(messageBody1.isOrangeLed(), messageBody2.isOrangeLed())
                        && Objects.equals(messageBody1.getFans(), messageBody2.getFans())
                        && Objects.equals(messageBody1.getBuzzer(), messageBody2.getBuzzer())
                        && SteppingMotor.same(messageBody1.getSteppingMotor(), messageBody2.getSteppingMotor());
            }
        }
    }

    @Data
    public static class SteppingMotor {
        private boolean clockwise = true;
        private double rpm = 0;
        private double circle = 0;

        public static boolean same(SteppingMotor steppingMotor1, SteppingMotor steppingMotor2) {
            if (Objects.isNull(steppingMotor1) && Objects.isNull(steppingMotor2)) {
                return true;
            } else if (Objects.isNull(steppingMotor1) || Objects.isNull(steppingMotor2)) {
                return false;
            } else {
                return Objects.equals(steppingMotor1.clockwise, steppingMotor2.isClockwise())
                        && Objects.equals(steppingMotor1.rpm, steppingMotor2.getRpm())
                        && Objects.equals(steppingMotor1.circle, steppingMotor2.getCircle());
            }
        }
    }


}
